package club.yuyang.campus2mall.controller;

import club.yuyang.campus2mall.entity.User;
import club.yuyang.campus2mall.service.UserService;
import club.yuyang.campus2mall.utils.Result;
import cn.hutool.crypto.SecureUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;

@RestController
public class LoginController {

    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;


//    用户登录校验
    @PostMapping("/login/check")
    public Result loginCheck(@RequestBody User user){
        String md5Password = SecureUtil.md5(user.getPassword());
        User dbUser = userService.queryByAccountAndPassword(user.getAccount(),md5Password);
        if (dbUser != null){
            String token = UUID.randomUUID()+"";

            User user1 = new User();
            user1.setId(dbUser.getId());
            user1.setToken(token);
            
            userService.updateById(user1);
            redisTemplate.opsForValue().set(token,user);
            return new Result(dbUser,"登录成功！","200");
        }
        return new Result(null,"登陆失败！","404");
    }
}
